import class_info
import re
import os

target_root = '/Users/apple/Desktop/1/'
name_list_root = '0217181_name_list.txt'
rename_button = 0


def main():
    my_class = class_info.Class(name_list_root, target_root)
    name_list = my_class.read_name_list()
    target_list = my_class.read_target_list()

    if rename_button:
        # 格式化文件名
        rename_file(name_list, target_list)
        # 刷新target_list数据
        target_list = my_class.read_target_list()

    # 检查上交情况
    absent_list = check(name_list, target_list)
    print_absent_list(absent_list)


def rename_file(name_list, target_list):
    for target in target_list:
        pattern = re.compile('[0123456789A]+')
        flag = pattern.search(target)
        try:
            for i in range(len(name_list)):
                if name_list[i][0] == flag.group():
                    file_type = target.split('.')[1]
                    os.rename(target_root+target, target_root+name_list[i][0] +
                              '-' + name_list[i][1] + '-电子电气工程学院-2020年产学合作教育工作学期调研报告'
                                                      + '.'+file_type)
                    break
        except Exception as e:
            print(target, e)


def check(name_list, target_list):
    print("总计%d位学生，" % len(name_list), end='')
    print("总共收到%d份，" % len(target_list), end='')

    absent_list = []
    for i in name_list:
        # i[0]为学号， i[1]为姓名
        k = 0
        for j in target_list:
            pattern = re.compile(i[0])
            flag = pattern.search(j)
            if flag:
                k = 1
                break
        if k == 0:
            absent_list.append(i)
    print("%d位没交" % len(absent_list))
    return absent_list


def print_absent_list(absent_list):
    if len(absent_list) == 0:
        return
    else:
        print("分别为：")
        for i in absent_list:
            print(i[0] + '\t' + i[1])


if __name__ == '__main__':
    main()